home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / RELEASE.ZIP / sub_arctic / constraints / op1_impl.java < prev    next >
Encoding:
Java Source  |  1996-10-04  |  13.5 KB  |  391 lines

  1. package sub_arctic.constraints;
  2.  
  3. import sub_arctic.lib.interactor;
  4. import sub_arctic.lib.interactor_consts;
  5. import sub_arctic.lib.manager;
  6. import sub_arctic.lib.sub_arctic_error;
  7.  
  8. import java.util.Vector;
  9.  
  10. /** 
  11.  * Constraint implementation class to provide encoding for standard 1 operand 
  12.  * lightweight constraint.  This object takes 1 std_objpart_encoding object 
  13.  * representing an operand, along with a signed 16 bit value to provide a 
  14.  * constant to the function.<p>
  15.  *
  16.  * 1 operand constraints are encoded as:
  17.  * <pre>
  18.  *             16          6     5     5
  19.  *     KKKKKKKKKKKKKKKK AAAAAA 00001 00001   31 1 operand operations
  20.  *                              ...            [K is 16 bit signed]
  21.  *     KKKKKKKKKKKKKKKK AAAAAA 11111 00001   
  22.  * </pre>
  23.  * where KKK represents a 16 bit signed constant, AAA represents the parameter 
  24.  * objects, and XXX represents bits used to encode an op code.<p>
  25.  *
  26.  * @author Scott Hudson
  27.  */
  28.  
  29. public class op1_impl implements std_encoding_consts {
  30.  
  31.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  32.  
  33.   /** 
  34.    * Create a standard constraint given an op_code, an obj/part designator, 
  35.    * and a constant.<p>
  36.    * 
  37.    * @param int                  op_code op code value for this operation.
  38.    * @param std_objpart_encoding op1     designator for operand 1
  39.    * @param short                K       value for 16 bit signed constant
  40.    */
  41.   public static std_constraint create(
  42.     int op_code, std_objpart_encoding op1, short K) 
  43. {
  44.       return new std_constraint(encode(op_code, op1, K), op1.orientation());
  45.     }
  46.  
  47.    //had:
  48.    //* @exception bad_constraint if part of the encoding is out of range.
  49.  
  50.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  51.  
  52.   /** 
  53.    * Do an encoding given an op_code, an obj/part designator, and a constant.<p>
  54.    * 
  55.    * @param int                  op_code op code value for this operation.
  56.    * @param std_objpart_encoding op1     designator for operand 1
  57.    * @param short                K       value for 16 bit signed constant
  58.    */
  59.   public static int encode(int op_code, std_objpart_encoding op1, short K) 
  60. {
  61.       if (op_code < OP1_MIN || op_code > OP1_MAX)
  62.     throw new sub_arctic_error(
  63.      "Unrecognized op code (" + op_code + ") used for 1 op constraint");
  64.  
  65.       return (((int)K)<<16) | ((op1.encoding() & OBJPART_MASK) << 10) | 
  66.           ((op_code<<OP1_SHIFT) & OP1_MASK)  | OP1_LOBITS;
  67.     }
  68.  
  69.    //had:
  70.    //* @exception bad_constraint if part of the encoding is out of range.
  71.  
  72.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  73.  
  74.   /** 
  75.    * Decode op_code from the given encoding.  Note: we assume this is 
  76.    * already known to be a 1-op constraint.  If not, the results will be 
  77.    * wrong.<p>
  78.    * 
  79.    * @param int enc the encoding for the constraint.
  80.    */
  81.   public static int op_code(int enc) { return (enc & OP1_MASK) >> OP1_SHIFT; }
  82.  
  83.   /** 
  84.    * Decode first operand from given encoding.  Note: we assume this is
  85.    * already known to be a 1-op constraint.  If not, the results will be 
  86.    * wrong.<p>
  87.    * 
  88.    * @param int enc the encoding for the constraint.
  89.    */
  90.   public static int op1(int enc) { return (enc >> 10) & OBJPART_MASK; }
  91.  
  92.   /** Decode constant from given encoding.   Note: we assume this is
  93.    * already known to be a 1-op constraint.  If not, the results will be 
  94.    * wrong.<p>
  95.    * 
  96.    * @param int enc the encoding for the constraint.
  97.    */
  98.   public static short const_val(int enc) { return (short)((enc>>16) & 0xffff); }
  99.  
  100.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  101.  
  102.   /* Part constants copied over from interactor_consts for convenience. */
  103.  
  104.   public static final int X = interactor_consts.X;
  105.   public static final int Y = interactor_consts.Y;
  106.   public static final int W = interactor_consts.W;
  107.   public static final int H = interactor_consts.H;
  108.  
  109.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  110.  
  111.   /**
  112.    * Evaluate an encoded constraint function given its operand and constant
  113.    * values.  We assume here that it is has already been determined that
  114.    * the constraint is a 1-op constraint.<p>
  115.    *
  116.    * @param int        enc         the encoding from the constraint
  117.    * @param interactor constr_obj  the object being constrained
  118.    * @param int        val1        value of first operand
  119.    * @param int        cnst_val    value of constant parameter
  120.    * @param int        orient      orientation of the constraint (should be
  121.    *                               HORIZONTAL or VERTICAL).
  122.    * @return int the result of the evaluation
  123.    */
  124.   public static int eval_fun(
  125.     int enc,
  126.     interactor constr_obj,
  127.     int        val1,
  128.     int        cnst_val,
  129.     int        orient) 
  130. {
  131.       interactor par;
  132.       int        self_wh;
  133.  
  134.       /* sanity check */
  135.       if (constr_obj == null) 
  136.     throw new sub_arctic_error("Null constrained object passed to " + 
  137.                 "op1_impl.eval_fun()");
  138.  
  139.       /* execute code for the encoded function */
  140.       switch(op_code(enc))
  141.     {
  142.       case OP_self_fun1:
  143.         /* use the object's custom_fun1() to compute the value */
  144.         return constr_obj.custom_fun1(val1, cnst_val);
  145.  
  146.       case OP_parent_fun1:
  147.         /* get the parent and call its custom_fun1() */
  148.         par = constr_obj.parent();
  149.         if (par != null)
  150.           return par.custom_fun1(val1, cnst_val);
  151.         else
  152.           return 0; 
  153.  
  154.       case OP_not_mask:
  155.         return ~val1 & (cnst_val | 0xffff0000);
  156.  
  157.       case OP_mask:
  158.         return val1 & (cnst_val | 0xffff0000);
  159.  
  160.       case OP_centered:
  161.         /* get self.wh */
  162.         if (orient == HORIZONTAL)
  163.           self_wh = constr_obj.get_part(W);
  164.         else
  165.           self_wh = constr_obj.get_part(H);
  166.  
  167.         /* compute and return value */
  168.         return (val1 - self_wh)/2 + cnst_val;
  169.  
  170.       case OP_offset:
  171.         return val1 + cnst_val;
  172.  
  173.       case OP_far_edge_just:
  174.         /* get self.wh */
  175.         if (orient == HORIZONTAL)
  176.           self_wh = constr_obj.get_part(W);
  177.         else
  178.           self_wh = constr_obj.get_part(H);
  179.  
  180.         /* compute and return value */
  181.         return val1 - self_wh - cnst_val;
  182.  
  183.       default:
  184.         /* something is wrong if we get here */
  185.         throw new sub_arctic_error("Improperly encoded constraint found in "+
  186.                      "op1_impl.eval_fun()");
  187.     }
  188.     }
  189.  
  190.    //had:
  191.    //* @exception bad_value if the encoding, or constrained object/part are 
  192.    //*                      malformed
  193.  
  194.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  195.  
  196.    /** 
  197.     * Evaluate an encoded constraint applied to the given part of the given
  198.     * object.  We assume here that it it has already been determined that
  199.     * the constraint is a 1-op constraint.<p>
  200.     *
  201.     * @param int        enc         the encoding from the constraint
  202.     * @param interactor constr_obj  the object being constrained
  203.     * @param int        constr_part the part being constrained
  204.     * @param int        orient      the orientation of the constraint
  205.     * @return int the result of the evaluation
  206.     */
  207.    public static int eval(
  208.      int        enc, 
  209.      interactor constr_obj, 
  210.      int        constr_part,
  211.      int        orient) 
  212. {
  213.        int cnst_val, val1; 
  214.  
  215.        /* sanity check */
  216.        if (constr_obj == null) 
  217.      throw new sub_arctic_error("Null constrained object passed to " + 
  218.                  "op1_impl.eval()");
  219.  
  220.        /* extract the constant */
  221.        cnst_val = const_val(enc);
  222.  
  223.        /* get the value for the single operand */
  224.        val1 = std_constraint_impl.the_impl().fetch_value(
  225.                         op1(enc), constr_obj, orient);
  226.  
  227.        /* compute the value */
  228.        return eval_fun(enc, constr_obj, val1, cnst_val, orient);
  229.  
  230.      }
  231.  
  232.     //had:
  233.     //* @exception bad_value if the encoding, or constrained object/part are 
  234.     //*                      malformed
  235.  
  236.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  237.  
  238.    /** Test whether the given encoded constraint (constraining the given 
  239.     *  object) depends on the indicated object and part (expressed relative
  240.     *  to it).  Here we assume that the encoding is already known to contain
  241.     *  a 1-op constraint.<p>
  242.     *
  243.     * @param int        enc             The encoding for the constraint.
  244.     * @param interactor constr_obj      The object the constraint is attached to
  245.     * @param int        test_which_obj  The object we are asking about.  This 
  246.     *                                   can be one of the values OBJCODE_SELF, 
  247.     *                                   OBJCODE_PARENT, OBJCODE_SOME_CHILD, 
  248.     *                                   OBJCODE_PREV_SIBLING, or 
  249.     *                                   OBJCODE_NEXT_SIBLING.  
  250.     * @param int        test_which_part The part we are asking about.
  251.     * @param int        nth_child       for SOME_CHILD, this provides the index
  252.     *                    of the child (and is ignored otherwise).
  253.     * @param int        orient          Orientation of the constraint.  This 
  254.     *                                   should be HORIZONTAL or VERTICAL.
  255.     * @return boolean whether the given constraint depends upon the given value
  256.     */
  257.    public static boolean depends_on(
  258.      int        enc, 
  259.      interactor constr_obj,
  260.      int        which_obj, 
  261.      int        which_part, 
  262.      int        nth_child,
  263.      int        orient) 
  264. {
  265.        int op;
  266.  
  267.        /* extract op code to look for special case implicit operands */ 
  268.        op = op_code(enc);
  269.  
  270.        /* only special cases here are  centered and far_edge_just which have 
  271.     * implicit self.wh */
  272.        if ((op == OP_centered || op == OP_far_edge_just) && 
  273.        which_obj == OBJCODE_SELF)
  274.      {
  275.            if (orient == HORIZONTAL)
  276.          {
  277.            if (which_part == W) return true;
  278.          }
  279.            else
  280.          {
  281.            if (which_part == H) return true;
  282.          }
  283.      }
  284.  
  285.        /* now have a look at the operand dependency */
  286.        op = op1(enc);
  287.        return std_constraint_impl.the_impl().part_depends_on(op, constr_obj, 
  288.                                which_obj, which_part, nth_child, orient);
  289.      }
  290.  
  291.     //had:
  292.     //* @exception bad_value if one of the parameters has an unrecognized value
  293.  
  294.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  295.  
  296.   /**
  297.    * Extract the set of objects/parts that a constraint depends on.  This
  298.    * produces a Vector with pairs of entries, the first being an interactor,
  299.    * and the second being an Integer which is the part number of that 
  300.    * interactor that is depended upon.<p>
  301.    *
  302.    * Here we assume that this is a 1 operand constraint.<p>
  303.    *
  304.    * @param int        enc        encoding value for the constraint in question.
  305.    * @param interactor constr_obj the object the constraint is attached to 
  306.    *                              (hence its referents are relative to).
  307.    * @param int        orient     Orientation of the constraint.  This 
  308.    *                              should be HORIZONTAL or VERTICAL.
  309.    * @return Vector containing pairs of objects, the first being an interactor
  310.    *                which is depended upon, and the second being an Integer
  311.    *                giving the part number of the part depended upon.
  312.    */
  313.   public static Vector mk_depend_list(int enc, interactor constr_obj,int orient) 
  314. {
  315.       int op;
  316.       Vector result;
  317.  
  318.       /* do the implicit special cases */
  319.       result = mk_implicit_depend_list(enc, constr_obj, orient);
  320.  
  321.       /* extract the operand and process that */
  322.       op = op1(enc);
  323.       std_constraint_impl.the_impl()
  324.              .add_depend_obj_part(result, op, constr_obj, orient);
  325.  
  326.       return result;
  327.     }
  328.  
  329.    //had:
  330.    //* @exception bad_value if one of the parameters has an unrecognized value
  331.  
  332.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  333.  
  334.   /**
  335.    * Extract the set of objects/parts that a particular constraint function 
  336.    * implicitly depends on.  This produces a Vector with pairs of entries, the 
  337.    * first being an interactor, and the second being an Integer which is the 
  338.    * part number of that interactor that is depended upon.<p>
  339.    *
  340.    * @param int        enc        encoding value for the constraint in question.
  341.    * @param interactor constr_obj the object the constraint is attached to 
  342.    *                              (hence its referents are relative to).
  343.    * @param int        orient     Orientation of the constraint.  This 
  344.    *                              should be HORIZONTAL or VERTICAL.
  345.    * @return Vector containing pairs of objects, the first being an interactor
  346.    *                which is depended upon, and the second being an Integer
  347.    *                giving the part number of the part depended upon.
  348.    */
  349.   public static Vector mk_implicit_depend_list(
  350.     int enc, interactor constr_obj,int orient)
  351.     {
  352.       int    op;
  353.       Vector result = new Vector(6);
  354.  
  355.       /* extract operand. */
  356.       op = op_code(enc);
  357.  
  358.       /* only special cases are OP_centered and OP_far_edge_just which 
  359.        * implicitly depend on self.wh. */
  360.       if (op == OP_centered || op == OP_far_edge_just)
  361.     {
  362.       result.addElement(constr_obj);
  363.       if (orient == HORIZONTAL)
  364.         result.addElement(new Integer(W));
  365.       else
  366.         result.addElement(new Integer(H));
  367.     }
  368.  
  369.       return result;
  370.     }
  371.  
  372.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  373. }
  374.  
  375. /*=========================== COPYRIGHT NOTICE ===========================
  376.  
  377. This file is part of the subArctic user interface toolkit.
  378.  
  379. Copyright (c) 1996 Scott Hudson and Ian Smith
  380. All rights reserved.
  381.  
  382. The subArctic system is freely available for most uses under the terms
  383. and conditions described in 
  384.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  385. and appearing in full in the lib/interactor.java source file.
  386.  
  387. The current release and additional information about this software can be 
  388. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  389.  
  390. ========================================================================*/
  391.